package com.wc.alorithm_blue_bridge._数据结构.单调栈.Q1142;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Stack;

/**
 * @Author congge
 * @Date 2023/11/4 13:19
 * @description 百亿富翁
 * https://www.lanqiao.cn/problems/1142/learning/?page=1&first_category_id=1&sort=students_count&second_category_id=8
 */
public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        Input sc = new Input();
        int n = sc.nextInt();
        int[] array = new int[n + 1];
        int[] l = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            array[i] = sc.nextInt();
            while (!stack.isEmpty() && array[stack.peek()] <= array[i]) {
                stack.pop();
            }
            if (stack.isEmpty()) {
                l[i] = -1;
            } else {
                l[i] = stack.peek();
            }
            stack.push(i);
        }
        stack.clear();
        int[] r = new int[n + 1];
        for (int i = n; i >= 1; i--) {
            while (!stack.isEmpty() && array[stack.peek()] <= array[i]) {
                stack.pop();
            }
            if (stack.isEmpty()) {
                r[i] = -1;
            } else {
                r[i] = stack.peek();
            }
            stack.push(i);
        }
        print(l);
        print(r);
    }

    static void print(int[] array){
        StringBuffer s = new StringBuffer();
        s.append(array[1]);
        for (int i = 2; i < array.length ; i++) {
            s.append(' ');
            s.append(array[i]);
        }
        System.out.println(s);
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }
    }

}
